package com.bdth.weget.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.weget.common.Pagination;
import com.bdth.weget.entity.Cars;
import com.bdth.weget.entity.Depart;
import com.bdth.weget.entity.Fleet;
import com.bdth.weget.mapper.CarsMapper;
import com.bdth.weget.mapper.FleetMapper;
import com.bdth.weget.service.CarsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/9.
 */
@SuppressWarnings("ALL")
@Service
public class CarsServiceImpl extends ServiceImpl<CarsMapper, Cars> implements CarsService {

    @Autowired
    private CarsMapper carsMapper;

    @Autowired
    private FleetMapper fleetMapper;

    @Override
    public Page<Cars> listPages(Pagination pagination, Map<String, Object> query) {
        EntityWrapper<Cars> entityWrapper = new EntityWrapper<>();
        if(query.get("simno")!=null)
            entityWrapper.like("simno",(String) query.get("simno"));
        if(query.get("carno")!=null)
            entityWrapper.like("carno",(String) query.get("carno"));
        if(query.get("plateno")!=null)
            entityWrapper.like("plateno",(String) query.get("plateno"));
        if(query.get("departids")!=null){
            String result = "";
            EntityWrapper<Fleet> fleetEntityWrapper = new EntityWrapper<>();
            fleetEntityWrapper.in("departid", (String)query.get("departids"));
            List<Fleet> list = fleetMapper.selectList(fleetEntityWrapper);
            for(Fleet bean : list){
                result+=bean.getId()+",";
            }
            //去掉最后一位逗号
            result = result.substring(0,result.length()-1);
            entityWrapper.in("fleetid",result);
        }
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Cars> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());
        List<Cars> list = baseMapper.selectPage(page,entityWrapper);
        page.setRecords(list);
        return page;
    }

    public String getUserCars(Map<String, Object> query){
        String cars = "";
        //去掉最后一位逗号
        cars = getUserCars((String)query.get("departids"));
        return cars;
    }

    public String getUserCars(String departids){
        EntityWrapper<Cars> entityWrapper = new EntityWrapper<>();
        String result = "";
        EntityWrapper<Fleet> fleetEntityWrapper = new EntityWrapper<>();
        fleetEntityWrapper.in("departid", departids);
        List<Fleet> list = fleetMapper.selectList(fleetEntityWrapper);
        for(Fleet bean : list){
            result+=bean.getId()+",";
        }
        //去掉最后一位逗号
        result = result.substring(0,result.length()-1);
        entityWrapper.in("fleetid",result);
        List<Cars> carsList = carsMapper.selectList(entityWrapper);
        String cars = "";
        for(Cars bean : carsList){
            cars+=bean.getId()+",";
        }
        //去掉最后一位逗号
        cars = cars.substring(0,cars.length()-1);
        return cars;
    }

    public Integer listTotal( Map<String, Object> query) {
        EntityWrapper<Cars> entityWrapper = new EntityWrapper<>();
        if(query.get("simno")!=null)
            entityWrapper.like("simno",(String) query.get("simno"));
        if(query.get("carno")!=null)
            entityWrapper.like("carno",(String) query.get("carno"));
        if(query.get("plateno")!=null)
            entityWrapper.like("plateno",(String) query.get("plateno"));
        Integer counts = carsMapper.selectCount(entityWrapper);
        return counts;
    }

    @Override
    public boolean update(Cars bean) {
        if(bean.getId()>0) {
            Integer result = carsMapper.updateById(bean);
            if(result>0)
                return true;
        }
        return false;
    }

    @Override
    public boolean save(Cars bean) {
        Integer result = carsMapper.insert(bean);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        Integer result = carsMapper.deleteById(id);
        if(result>0)
            return true;
        return false;
    }

    @Override
    public boolean remove(String ids) {
        String[] id = ids.split(",");
        Integer result = 0;
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            Integer r = carsMapper.deleteById(intId);
            if(r>0)
                result++;
        }
        if(result>0)
            return true;
        return false;
    }

    @Override
    public List<Cars> listAll() {
        EntityWrapper<Cars> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("status",1);
        return carsMapper.selectList(entityWrapper);
    }
}
